import {onBeforeRouteUpdate} from 'vue-router'
import { useRoute } from 'vue-router';
import { getCategoryAPI } from '@/apis/category';
import { onMounted,ref } from 'vue';

export function useCategory() {
  //获取面包屑分类数据
  const router = useRoute();
  const data = ref({});
  //第二种解决路由缓存的方式 
  const getDate = async (id = router.params.id) => {
    //这样写不行
    // data = await getCategoryAPI(router.params.id).result;
    const temp = await getCategoryAPI(id);
    data.value = temp.result;
    // console.log('data=', data.value);
  }

  onBeforeRouteUpdate((to) => {
    getDate(to.params.id);
  })

  onMounted(() => {
    getDate();
  })

  return { data }
}